%% based on albrecht et al ier 2007
% investors enter as buyers, return as sellers
close all; clear all;

%% upload exogenous parameters
exog
E = [r, d_r, beta, d_beta, x, eps, lambda, tau, di, DD, II];

%% simulating different diversity of housing
eps_vec = 0:(x/30):(x*0.6);
sp = length(eps_vec);

XX = zeros(sp, 9); % vector of endog variables
% V^h, V^d, V^ib, V^is, V^ih, B^h, B^i, S^i, H^i
XXD = zeros(sp, 1); % values of developers minus cost
TT = zeros(sp, 6); % vector of transcation dummies: whether X transacts with Y

% initial values to solve for eqm endog vars
x0 = [beta/r, (beta+d_beta)/r, (beta+di)/r, (beta+d_beta)/(r+d_r), 0, II/2, II/2, II/2, 0]; 

% loop to solve for different values of homogeneity, -epsilon
for ii = 1:sp
    E(6) = eps_vec(ii); % new epsilon
    fun = @(X) eq_eqs(X, E); % eqm conditions
    XX(ii, :) = fsolve(fun, x0); % solve
    x0 = XX(ii, :);     % reset initial guess
    TT(ii, :) = trade(XX(ii, :), E); % save transaction dummies to understand who trades with whom

    XXD(ii) = XX(ii, 2) - a*E(5) - b*E(6)^2; % correct 
end


%% plot values
f1 = figure(1);

left_color = [.5 .5 0];
right_color = [0 0 0];
set(f1,'defaultAxesColorOrder',[left_color; right_color]);

hold on
    rectangle('Position', [-1,-10,10,100], 'FaceColor', [0.9 0.9 0.9], 'EdgeColor', [0.9 0.9 0.9])
    scatter(eps_vec, XX(:,1), 'k', 'filled', 'LineWidth', 1.5)
    set(gca,'YTick', [])
    yyaxis right
    scatter(eps_vec, XXD, 'k+', 'LineWidth', 1.5)
    scatter(eps_vec, XX(:,3), 'k', 'LineWidth', 1.25)
    txt1 = 'II';
    text(eps_vec(3)-2,0,txt1,'FontSize',34)
    txt2 = 'I';
    text(eps_vec(7),0,txt2,'FontSize',34)
    xlim([eps_vec(1)-1 eps_vec(end)+1])
hold off

legend('End-user - searching','Developer','Investor - searching', ...
    'Location', 'west', 'interpreter', 'latex', 'FontSize', 14)
%ylabel('Value', 'interpreter', 'latex', 'FontSize', 14)
xlabel('Homogeneity measure, $-\epsilon$', 'interpreter', 'latex', 'FontSize', 14)
set(gca,'XTick', [],'YTick', [], 'xdir', 'reverse')


%% plot values
f2 = figure(2);

hold on
    scatter(eps_vec, XX(:,3), 'k', 'filled', 'LineWidth', 1.5)
    set(gca,'YTick', [])
    yyaxis right
    scatter(eps_vec, XX(:,4), 'k.', 'LineWidth', 1.5)
    scatter(eps_vec, XX(:,5), 'k', 'LineWidth', 1.25)
hold off

legend('Investor - searching','Investor - selling','Investor - holding', ...
    'Location', 'south', 'interpreter', 'latex', 'FontSize', 14)
%ylabel('Value', 'interpreter', 'latex', 'FontSize', 14)
xlabel('Homogeneity measure, $-\epsilon$', 'interpreter', 'latex', 'FontSize', 14)
set(gca,'XTick',[], 'YTick', [], 'xdir', 'reverse')


%% plot endog shares
f3 = figure(3);

hold on
    bar(eps_vec, XX(:,6), 'FaceColor', [0.3010 0.7450 0.9330])
hold off

title('Share of end-users searching', 'interpreter', 'latex', 'FontSize', 18)
ytickformat('%.2f')
xlabel('Homogeneity measure, $-\epsilon$', 'interpreter', 'latex', 'FontSize', 14)
set(gca,'XTick',[], 'xdir', 'reverse')

%%
f4 = figure(4);

bar(XX(:,7:9),'stacked')

legend('Mass of investors searching',  'Mass of investors selling','Mass of investors holding', ...
    'Location', 'north', 'interpreter', 'latex', 'FontSize', 14)
title('Masses of investors as a ratio to the mass of end-users', 'interpreter', 'latex', 'FontSize', 14)
ytickformat('%.2f')
ylim([0, II])
xlabel('Homogeneity measure, $-\epsilon$', 'interpreter', 'latex', 'FontSize', 14)
set(gca,'XTick',[], 'YTick',[], 'xdir', 'reverse')


%% what would developers choose under different number of investors?

VD = zeros(sp, 3); % vector of developers values
MI = [0.25, 0.5, 1]; % masses of investors

for im = 1:3
    
    E(11) = MI(im);

    x0 = [beta/r,  (beta+d_beta)/r,   (beta+di)/r, (beta+d_beta)/(r+d_r),    0, ...
    E(11), E(11)/3, E(11)/3, 0]; % initial values to solve for eqm endog vars

    for ii = 1:sp
        aux = zeros(sp, 9);
        E(6) = eps_vec(ii);
        fun = @(X) eq_eqs(X, E);
        aux(ii, :) = fsolve(fun, x0);
        x0 = aux(ii, :);     % reset initial guess
        VD(ii, im) = aux(ii, 2) - E(5)/4 - 0.004*E(6)^2;
    end

end

f5 = figure(5);

hold on
    scatter(eps_vec, VD(:,1), 'k.')
    scatter(eps_vec, VD(:,2), 50, 'k')
    scatter(eps_vec, VD(:,3), 50, 'k', 'filled')
    for im = 1:3
        [~,imx]=max(VD(:,im));
        plot(eps_vec(imx), VD(imx, im),'or', 'MarkerSize',18)
    end
hold off

legend('Mass of Investors = 0.25','Mass of Investors = 0.50','Mass of Investors = 1.00', ...
    'Location', 'northeast', 'interpreter', 'latex', 'FontSize', 14)
xlabel('Homogeneity measure, $-\epsilon$', 'interpreter', 'latex', 'FontSize', 14)
%ylim([-5, 35])
set(gca,'XTick',[], 'YTick', [], 'xdir', 'reverse')


%% EXTRA
%% matching function
%matching('mtchng', XX(:,6)+XX(:,7), DD+XX(:,8))

%% plot prices

PP = nan(sp, 6);

for ii = 1:sp
        PP(ii, 1) = (x - eps_vec(ii) - XX(ii,1) + XX(ii,2))/2;
        PP(ii, 2) = (x + eps_vec(ii) - XX(ii,1) + XX(ii,2))/2;
        PP(ii, 3) = (x - eps_vec(ii) - XX(ii,1) + XX(ii,4))/2;
        PP(ii, 4) = (x + eps_vec(ii) - XX(ii,1) + XX(ii,4))/2;
        PP(ii, 5) = (XX(ii,5) + XX(ii,2) - XX(ii,3))/2;
        PP(ii, 6) = (XX(ii,5) - XX(ii,3) + XX(ii,4))/2;
    for jj = 1:6
        if TT(ii, jj) == 0
            PP(ii, jj) = nan;
        end
    end
end


% prices for different combinations of value and seller type
% figure(6);
% plot(eps_vec, [PP mean(PP.*TT, 2, 'omitnan')])
% legend('p_e_d_m','p_e_d_p','p_e_i_m','p_e_i_p', 'p_i_d','p_i_i','average price')


%% average price by type of seller

PP_type = [mean(PP(:,[1 2 5]).*TT(:,[1 2 5]), 2, 'omitnan') mean(PP(:,[3 4 6]).*TT(:,[3 4 6]), 2, 'omitnan')];

figure(6);
hold on
    scatter(eps_vec, PP_type(:,1), 'k.', 'LineWidth', 1.5)
    scatter(eps_vec, PP_type(:,2), 'k', 'LineWidth', 1.25)
hold off
legend('Developer', 'Investor', 'Location', 'northeast', 'interpreter', 'latex', 'FontSize', 14)
ylabel('Average price - by seller type', 'interpreter', 'latex', 'FontSize', 14)
xlabel('Homogeneity measure, $-\epsilon$', 'interpreter', 'latex', 'FontSize', 14)
set(gca,'XTick',[], 'YTick', [], 'xdir', 'reverse')

%% liquidity premium disappearing for investors
figure(7);
plot(eps_vec, mean(PP(:,[3 4 6]).*TT(:,[3 4 6]), 2, 'omitnan')./mean(PP(:,[5:6]).*TT(:,[5:6]), 2, 'omitnan'), 'k')
ylabel('Price of selling to price of purchasing - investor', 'interpreter', 'latex', 'FontSize', 14)
xlabel('Homogeneity measure, $-\epsilon$', 'interpreter', 'latex', 'FontSize', 14)
set(gca,'XTick',[], 'xdir', 'reverse')

%% average price by type of buyer
PP_type = [mean(PP(:,[1:4]).*TT(:,[1:4]), 2, 'omitnan') mean(PP(:,[5:6]).*TT(:,[5:6]), 2, 'omitnan')];

figure(8);
hold on
    scatter(eps_vec, PP_type(:,1), 'k.', 'LineWidth', 1.5)
    scatter(eps_vec, PP_type(:,2), 'k', 'LineWidth', 1.25)
hold off
legend('End-user', 'Investor', 'Location', 'southwest', 'interpreter', 'latex', 'FontSize', 14)
ylabel('Average price - by buyer type', 'interpreter', 'latex', 'FontSize', 14)
xlabel('Homogeneity measure, $-\epsilon$', 'interpreter', 'latex', 'FontSize', 14)
set(gca,'XTick',[], 'YTick', [], 'xdir', 'reverse')